package com.gitee.huanminabc.db_freedom.wrapper;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class SQL {
    private StringBuilder sql = new StringBuilder();
    private List<Object> params = new ArrayList<>();

    public SQL set(SQL sql){
        this.sql=sql.sql;
        this.params=sql.params;
        return this;
    }

    public SQL addParam(Object param){
        params.add(param);
        return this;
    }

    public SQL addParamAll(List<Object> param) {
        params.addAll(param);
        return this;
    }

    public int  length(){
        return sql.length();
    }

    public SQL append(String str){
        sql.append(str);
        return this;
    }
    public SQL append(SQL sql){
        this.sql.append(sql.sql);
        return this;
    }
    public SQL append(Object obj){
        //这种就不添加了,传入的不能是对象否则就自行处理了
        return this;
    }
    public SQL append(int i){
        sql.append(i);
        return this;
    }
    public SQL append(long l){
        sql.append(l);
        return this;
    }
    public SQL append(char c){
        sql.append(c);
        return this;
    }
    public SQL append(boolean b){
        sql.append(b);
        return this;
    }
    public SQL append(float f){
        sql.append(f);
        return this;
    }
    public SQL append(double d){
        sql.append(d);
        return this;
    }
    public SQL append(char[] chars){
        sql.append(chars);
        return this;
    }
    public SQL append(char[] chars,int offset,int len){
        sql.append(chars,offset,len);
        return this;
    }
    public SQL append(CharSequence charSequence){
        sql.append(charSequence);
        return this;
    }
    public SQL append(CharSequence charSequence,int start,int end){
        sql.append(charSequence,start,end);
        return this;
    }

    public void deleteCharAt(int index){
        sql.deleteCharAt(index);
    }

    //toString
    public String toString(){
        String stringSql = sql.toString();
        //将sql中的?替换成真实的参数
        for (Object param : params) {
            stringSql = stringSql.replaceFirst("\\?", param.toString());
        }
        return stringSql;
    }


}
